<?php

class Blog_ArticleController extends Ext_Controller_Action_Front
{
    public function init()
    {
        parent::init();
    }
    
    public function preDispatch() {
        parent::preDispatch();
        
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        $roleId = Ext_User::getInstance()->show($userId)->roleId;
        $acl = Ext_Acl::getInstance()->build($roleId);
        
        if(!$acl->isAllowed($roleId, 'blog', 'view')){
            $this->_forward("show-access-denied", "auth", "account");
            return false;
        }
    }

    public function viewAction()
    {
        $articleId = $this->filter->filter($this->_request->get('articleId'));
        if(!$articleId)
        {
            $viewResult = new Ext_View_Result();
            $viewResult->addMessage("Access denied.");
            $viewResult->setNoRender();
            return "";
        }
        
        $article = Ext_Article::getInstance()->show($articleId)->buildDescription()->buildAttachemnts()->buildTags()->buildFamily();
        if(isset($article->author)){
            $author = Ext_User::getInstance()->show($article->author->userId)->buildAvatar()->buildUrl();
            $buddyCheckor = new Ext_Buddy_Check($article->author->userId);
            $isMyBuddy = $buddyCheckor->isMyBuddy();
        }
        
        $this->view->pageTitle = $article->title;
        if(isset($article->author)){
            $this->view->author    = $author;
            $this->view->isMyBuddy = $isMyBuddy;
        }
        $this->view->article   = $article;
        
    }

    public function listAction()
    {
        $domain = $this->filter->filter($this->_request->get('domain')); 
        $userId = $this->filter->filter($this->_request->get('userId')); 
        if(!$domain && !$userId)
        {
            $viewResult = new Ext_View_Result();
            $viewResult->addMessage("Access denied.");
            $viewResult->setNoRender();
            return "";
        }
        
        if($domain){
            $domainOne = App_Model_DbTable_Domain::getInstance()->getRowByDomain($domain);
            if($domainOne)
            {
                $userId = $domainOne['userId'];
            }
        }
        
        $author = Ext_User::getInstance()->show($userId)->buildAvatar()->buildUrl();
        
        $buddyCheckor = new Ext_Buddy_Check($userId);
        $isMyBuddy = $buddyCheckor->isMyBuddy();

        
        $count = App_Model_Article::getInstance()->getMyBlogCount($userId);
        $list = App_Model_Article::getInstance()->getMyBlogList($userId);
        $adapter = new Zend_Paginator_Adapter_DbSelect($list);
        $adapter->setRowCount($count);
        $paginator = new Zend_Paginator($adapter);
        $paginator
              //->setCurrentPageNumber(1)
              ->setItemCountPerPage(8)
              ->setCurrentPageNumber($this->_getParam('page'))
            //->setDefaultPageRange(20)
            ;
        $articles = array();
        foreach ($paginator as $key=>$item)
        {
            $articleId = $item['articleId'];
            
            $article = Ext_Article::getInstance()->show($articleId)->isExcerpt()->buildDescription()->buildAttachemnts()->buildTags()->buildFamily();
            
            $articles[] = $article;
        }

        $this->view->author    = $author;
        $this->view->isMyBuddy = $isMyBuddy;
        $this->view->articles  = $articles;
        $this->view->paginator = $paginator;
        $this->view->requestParams  = $this->_request->getParams();
    }
}

